//
//  NYWrongSubjectBookViewController.m
//  NiuWaJiaoYu
//
//  Created by MinLison on 2018/1/5.
//  Copyright © 2018年 niuwaketang. All rights reserved.
//

#import "NYWrongSubjectBookViewController.h"
#import "NYPageViewController.h"
#import "NYSubjectListViewController.h"

#import "NYWrongBottomTableCellModel.h"
#import "NYSubjectListRequest.h"
#import "NYSubjectBookPageInfoCell.h"
#import "BaseTableHeaderFooterView.h"

@interface NYWrongSubjectBookViewController ()<WGPageViewControllerDelegate, QMUITableViewDelegate>
@property(nonatomic, strong) NYPageViewController *pageViewController;
@property(nonatomic, strong) NYWrongBottomTableCellModel *contentCellModel;
@property(nonatomic, strong) QMUIButton *topSortRightBtn;
@property(nonatomic, strong) NYSubjectListModel *subjectList;
@end

@implementation NYWrongSubjectBookViewController

- (void)didInitializedWithStyle:(UITableViewStyle)style
{
        self.contentCellModel = [[NYWrongBottomTableCellModel alloc] init];
        [self configPageViewController];
        [super didInitializedWithStyle:style];
}

- (void)viewDidLoad
{
        [super viewDidLoad];
        [self loadData];
}
- (void)configNavigationBar:(BaseNavigationBar *)navigationBar
{
        [super configNavigationBar:navigationBar];
        self.title = @"错题本";
}

- (void)loadData
{
        [self setLoading:YES animation:YES];
        [self configNimbus];
        [[NYSubjectListRequest requestWithType:(NYSubjectListTypeWrongSubject) params:nil] startWithSuccess:^(__kindof BaseRequest * _Nonnull request, __kindof NYSubjectListModel * _Nonnull data) {
                self.subjectList = data;
                [self configNimbusData];
                [self setSuccess];
        } failed:^(__kindof BaseRequest * _Nonnull request, NSError * _Nonnull error) {
                [self setError:error];
        }];
}
- (void)configNimbus
{
        [super configNimbus];
        [self.tableViewActions forwardingTo:self];
        [self.tableView registerClass:[BaseTableHeaderFooterView class] forHeaderFooterViewReuseIdentifier:[BaseTableHeaderFooterView className]];
}
- (void)configNimbusData
{
        [self.tableViewModel addSectionWithTitle:nil];
        [self.tableViewModel addObject:self.subjectList.pageInfo];
        [self.tableViewModel addSectionWithTitle:nil];
        [self.tableViewModel addObject:self.contentCellModel];
        [self.tableView reloadData];
        [self reloadPageViewController];
}
- (void)reloadPageViewController
{
        NSMutableArray *tmp = [NSMutableArray arrayWithCapacity:self.subjectList.category.count];
        [tmp addObject:@"全部"];
        [self.subjectList.category enumerateObjectsUsingBlock:^(NYSubjectCategoryModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                [tmp addObject:obj.name];
        }];
        self.pageViewController.segmentTitles = tmp;
        [self.pageViewController reloadData];
}
- (void)configPageViewController
{
        self.pageViewController = [[NYPageViewController alloc] init];
//        self.pageViewController.scrollEnable = NO;
        [self.pageViewController willMoveToParentViewController:self];
        [self addChildViewController:self.pageViewController];
        self.contentCellModel.commentContentView = self.pageViewController.view;
        self.pageViewController.customTopSortView = YES;
        [self.pageViewController didMoveToParentViewController:self];
        self.pageViewController.pageDelegate = self;
        self.pageViewController.topSortBottomLineColor = UIColorSeparator;
        self.pageViewController.segmentControl.segmentWidthStyle = HMSegmentedControlSegmentWidthStyleDynamic;
        self.pageViewController.segmentControl.selectedTitleTextAttributes = @{
                                                                               NSForegroundColorAttributeName : UIColorHex(0x5396F7),
                                                                               NSFontAttributeName : WGSystem14Font,
                                                                               };
        self.pageViewController.segmentControl.titleTextAttributes = @{
                                                                       NSForegroundColorAttributeName : UIColorHex(0x333333),
                                                                       NSFontAttributeName : WGSystem14Font,
                                                                       };
        self.pageViewController.segmentControl.selectionIndicatorEdgeInsets = UIEdgeInsetsMake(0, -3, 0, -3);
        self.pageViewController.segmentControl.selectionIndicatorColor = UIColorHex(0x5396F7);
        self.pageViewController.segmentControl.segmentEdgeInset = UIEdgeInsetsMake(0, 10, 0, 10);
        self.pageViewController.segmentLayoutEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
//        [self configTopSortView];
}
- (void)configTopSortView
{
        [self.pageViewController.topSortView addSubview:self.topSortRightBtn];
        [self.topSortRightBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.top.right.bottom.equalTo(self.pageViewController.topSortView);
                make.width.mas_equalTo(__WGWidth(40));
        }];
}
/// MARK: - TableView Delegate
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
        if (section == 1)
        {
                return __WGHeight(40);
        }
        return 0.0001;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
        if (section == 0)
        {
                return __WGHeight(10);
        }
        return 0.0001;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
        BaseTableHeaderFooterView *view = [tableView dequeueReusableHeaderFooterViewWithIdentifier:[BaseTableHeaderFooterView className]];
        view.contentView.backgroundColor = UIColorForBackground;
        return view;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
        BaseTableHeaderFooterView *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:[BaseTableHeaderFooterView className]];
        [header.contentView removeAllSubviews];
        if (section == 1)
        {
                [self.pageViewController.topSortView removeFromSuperview];
                [header addSubview:self.pageViewController.topSortView];
                [self.pageViewController.topSortView mas_remakeConstraints:^(MASConstraintMaker *make) {
                        make.edges.equalTo(header);
                }];
                return header;
        }
        return header;
}
/// MARK: - PageController Delegate
- (NSInteger)countOfContentItems
{
        return self.pageViewController.segmentTitles.count;
}

- (UIViewController *)contentControllerAtIndex:(NSInteger)index
{
        NYSubjectListViewController *vc = [[NYSubjectListViewController alloc] init];
        vc.category_id = [(NYSubjectCategoryModel *)[self.subjectList.category jk_objectWithIndex:index - 1] id];
        vc.subjectListType = NYSubjectListTypeWrongSubject;
        return vc;
}

- (void)configController:(NYSubjectListViewController *)controller atIndex:(NSInteger)index
{
        
}
- (void)controllerDidShow:(NYSubjectListViewController *)controller atIndex:(NSInteger)index
{
        @weakify(self);
        [self.KVOController observe:controller.tableView keyPath:@keypath(controller.tableView,contentOffset) options:(NSKeyValueObservingOptionNew) block:^(NYWrongSubjectBookViewController *  _Nullable observer, BaseTableView *  _Nonnull object, NSDictionary<NSString *,id> * _Nonnull change) {
                @strongify(self);
                
                CGPoint offset = [change[NSKeyValueChangeNewKey] CGPointValue];
                if ( CGPointEqualToPoint(offset, CGPointZero) )
                {
                        return;
                }
                /// 上移
                CGRect section1Rect = [self.tableView rectForHeaderInSection:1];
                if (offset.y > 0 && self.tableView.contentOffset.y < section1Rect.origin.y - 1)
                {
                        [self.tableView scrollToRow:0 inSection:1 atScrollPosition:(UITableViewScrollPositionBottom) animated:YES];
                        [object setContentOffset:CGPointZero animated:NO];
                        return;
                }
                else if (offset.y < 0 )
                {
                        [self.tableView scrollToRow:0 inSection:0 atScrollPosition:(UITableViewScrollPositionTop) animated:YES];
                        [object setContentOffset:CGPointZero animated:NO];
                        return;
                }
        }];
}

- (BOOL)hasTopSegment
{
        return YES;
}
- (QMUIButton *)topSortRightBtn
{
        if (_topSortRightBtn == nil) {
                _topSortRightBtn = [[QMUIButton alloc] qmui_initWithImage:[UIImage wo_de_kao_shi_xiang_you_jian_tou] title:nil];
                _topSortRightBtn.qmui_borderPosition = QMUIBorderViewPositionLeft | QMUIBorderViewPositionBottom;
        }
        return _topSortRightBtn;
}

/// NavBar
+ (NYViewControllerNavigationBarType)navigationBarType
{
        return NYViewControllerNavigationBarTypeBlue;
}
@end
